<!--
* @description 消息提醒设置
* @author zywu
* @date 2021/04/23 09:46:40
!-->
<template>
  <div class="list-content">
    <el-row type="flex" justify="space-between">
      <el-form :inline="true" :model="formInline" class="demo-form-inline" @submit.native.prevent>
        <el-form-item>
          <el-select @change="getList(1)" v-model="formInline.status" placeholder="请选择状态" clearable>
            <el-option v-for="item in statusList" :key="item.value" :label="item.text" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="isMuilUnitUser">
          <el-select filterable @change="getList(1)" v-model="formInline.orgId" placeholder="请选择联网单位" clearable>
            <el-option
              v-for="item in orgOptions"
              :key="item.organizationId"
              :label="item.shortName"
              :value="item.organizationId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="formInline.time"
            type="datetimerange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm"
            :default-time="['00:00:00','23:59:59']"
            :picker-options="pickerOptions"
            @change="getList(1)"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-input v-model.trim="formInline.name" placeholder="请输入设备名称" clearable @keyup.enter.native="getList(1)"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="form-search-btn" type="primary" @click="getList(1)">查询</el-button>
        </el-form-item>
      </el-form>
      <div class="more-btns">
        <el-button type="primary" @click="handleEdit()">新增</el-button>
      </div>
    </el-row>
    <el-table v-loading="loading" :data="tableData" ref="mainTable" style="width: 100%">
      <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template v-slot="sc">
          <el-tag
            :type="['info','primary','danger'][(sc.row.status||1)-1]"
          >{{(statusList.filter(n=>{ return n.value==(sc.row.status||1) })[0]||{}).text}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="orgName" label="联网单位"></el-table-column>
      <el-table-column prop="unitFloorName" label="单位楼栋"></el-table-column>
      <el-table-column prop label="屏蔽时间" width="300px">
        <template
          v-slot="sc"
        >{{(sc.row.monitorStartTime||'').substr(0,16)}}至{{(sc.row.monitorEndTime||'').substr(0,16)}}</template>
      </el-table-column>
      <el-table-column prop="monitorFacilityNum" label="屏蔽设备数" width="100" align="center"></el-table-column>
      <el-table-column prop="monitorStatus" :formatter="formatStatus" label="屏蔽设备监测状态"></el-table-column>
      <el-table-column prop="createUserName" label="创建人"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.$index, scope.row,true)" type="text" size="small">详情</el-button>
          <span class="dividing-line">|</span>
          <el-button @click="handleEdit(scope.$index, scope.row)" type="text" size="small">编辑</el-button>
          <span class="dividing-line">|</span>
          <el-button @click="handleDel(scope.$index, scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="sizeChange"
      @current-change="pageChange"
      :current-page="currentPageIndex"
      :page-sizes="[10, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <el-dialog
      :close-on-click-modal="false"
      title="消息提醒设置"
      width="1000px"
      top="10vh"
      v-if="dialogFalg"
      :visible.sync="dialogFalg"
      @close="closeDialog"
      v-dialogDrag
    >
      <edit-form ref="editForm" :editInfo="editInfo" :readonly="readonly" :closeDialog="closeDialog"></edit-form>
      <div slot="footer" class="dialog-footer" style="padding-top:10px;">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button v-if="!readonly" type="primary" @click="handleSave">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMsgAlertList, deleteMsgAlert } from '@/api/sysConfig'
import util from '@/utils/util'
import editForm from './PushConfigEdit'
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'PushConfig',
  components: {
    editForm,
  },
  data() {
    return {
      statusList: [
        { text: '未开始', value: '1' },
        { text: '进行中', value: '2' },
        { text: '到期失效', value: '3' },
      ],
      formInline: {
        orgId: '',
        name: '',
        status: '',
      },
      total: 0,
      currentPageSize: 10,
      currentPageIndex: 1,
      tableData: [], //表格数据
      detailObj: {}, //展开详情
      loading: false,
      dialogFalg: false,
      editInfo: {}, // 编辑对应的表单信息
      pickerOptions: {},
      readonly: false,
    }
  },
  computed: {
    ...mapState(['userinfo', 'orgOptions']),
    ...mapGetters(['isMuilUnitUser', 'userOrgId', 'userOrgIds']),
  },
  created: function () {
    this.getList()
  },
  methods: {
    formatStatus(row, col, val) {
      let map = {
        10: '报警',
        20: '故障',
        40: '联动',
      }
      return val
        .split(',')
        .map((n) => {
          return map[n]
        })
        .join(',')
    },
    handleSave() {
      this.$refs.editForm.saveEdit()
    },
    sizeChange(_pageSize) {
      //页码切换
      this.getList(1, _pageSize)
    },
    pageChange(_pageIndex) {
      //翻页
      this.getList(_pageIndex)
    },
    getList(_pageIndex, _pageSize) {
      //获取列表
      if (_pageIndex) {
        this.currentPageIndex = _pageIndex
      }
      if (_pageSize) {
        this.currentPageSize = _pageSize
      }
      this.loading = true
      getMsgAlertList({
        status: this.formInline.status,
        orgId: this.formInline.orgId,
        startTime: (this.formInline.time || ['', ''])[0],
        endTime: (this.formInline.time || ['', ''])[1],
        queryType: 1, //0或空不做分页 1做分页
        pageIndex: this.currentPageIndex - 1,
        pageSize: this.currentPageSize,
        structureName: this.formInline.name,
      }).then((result) => {
        this.total = result.total
        this.tableData = result.data
        this.loading = false
      })
    },
    handleEdit(index, row, readonly = false) {
      this.readonly = readonly
      this.dialogFalg = true
      this.editInfo = row || {}
    },
    handleDel(index, row) {
      let that = this
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        roundButton: true,
      })
        .then(() => {
          deleteMsgAlert({
            id: row.id,
            updateUser: this.userinfo.accountName,
          }).then((res) => {
            if (res.status === 200) {
              that.$message({
                type: 'success',
                message: '删除成功!',
              })
              that.getList(1)
            } else {
              that.$message.error(res.info)
            }
          })
        })
        .catch(() => {})
    },
    closeDialog(refresh) {
      this.dialogFalg = false
      if (refresh) {
        this.getList(this.editInfo.id ? '' : 1)
      }
    },
  },
}
</script>